import java.util.PriorityQueue;

public class Sort {


    private static void swap(int i, int j) {
        int tam = i;
        i = j;
        j = tam;
    }

    public static void insertSort(int[] array) {
        for (int i = 1; i <array.length; i++) {
            int tam = array[i];
            int j =i-1;
            for (; j >= 0; j--) {
                if (array[j] > tam) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tam;
        }
    }

    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array, gap);
        }

    }

    public static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tam = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tam) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tam;
        }
    }

    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[minIndex] > array[j]) {
                    minIndex = j;
                }
            }
            int tam = array[i];
            array[i] = array[minIndex];
            array[minIndex] = tam;
        }

    }

    public static void Craet(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            siftDown(parent, array, array.length);
        }
    }

    public static void siftDown(int parent, int[] array, int end) {
        int child = parent * 2 + 1;
        while (child < end) {
            if (child + 1 < end && array[child] < array[child + 1]) {
                child++;
            }
            if (array[child] > array[parent]) {
                int tam = array[child];
                array[child] = array[parent];
                array[parent] = tam;
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    public static void heapSort(int[] array) {
        Craet(array);
        int end = array.length - 1;
        while (end >= 0) {
            int tam = array[0];
            array[0] = array[end];
            array[end] = tam;
            siftDown(0, array, end);
            end--;
        }
    }

    public static void quickSort(int[] array) {
        quick(array,0, array.length - 1);
    }

    private static void quick(int[] array, int left, int right) {
        if(left>=right){
            return;
        }
        int par = paration(array, left, right);
        quick(array,left,par-1);
        quick(array,par+1,right);
    }

    private static int paration(int[] array, int left, int right) {
        int i=left;
        int tmp = array[left];
        while (left<right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            int tam = array[left];
            array[left] = array[right];
            array[right] = tam;
        }
        int n=tmp;
        array[i]=array[left];
        array[left]=n;
        return left;
    }
    public static void bubbleSort(int[] array){
        for(int i=0;i<array.length-1;i++){
            for (int j=i+1;j<=array.length-1;j++){
                if(array[j-1]>array[j]){
                    int tam=array[j-1];
                    array[j-1]=array[j];
                    array[j]=tam;
                }
            }
        }
    }
    public static void mergeSort(int[]array){
        int left=0;
        int right=array.length-1;
        mergeSortFun(array,left,right);
    }
    private static void mergeSortFun(int []array,int left,int right){
        if(left>=right){
            return;
        }
        int mid=(left+right)/2;
        mergeSortFun(array,left,mid);
        mergeSortFun(array,mid+1,left);

        merge(array,left,right,mid);
    }
    private static void merge(int[]array,int left,int right,int mid){
        int[] tmp=new int[right-left+1];
        int k=0;
        int s1=left;
        int end1=mid;
        int s2=mid+1;
        int end2=right;

        while(s1<=end1&&s2<=end2){
           if(array[s1]<=array[s2]) {
               tmp[k++] = array[s1++];
           }
            else{
                tmp[k++]=array[s2++];
            }
        }
        while(s1<=end1){
            tmp[k++]=array[s1++];
        }
        while(s2<=end2){
            tmp[k++]=array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+left]=tmp[i];
        }
    }

}

